var sorl = sorl || {}
sorl.curator = sorl.curator || {};

sorl.curator.DDApp = {

    list_ids: ['src', 'dest'],
    
    init: function() {
        for (var i=0; i<this.list_ids.length; i++) {
            new YAHOO.util.DDTarget(this.list_ids[i]);
            elems = YAHOO.util.Selector.query('#'+ this.list_ids[i] +' li');
            for (var j=0; j<elems.length; j++) {
                new sorl.curator.DDList(elems[j].id);
            }
        }
        // add edit listeners
        var dest = YAHOO.util.Selector.query('#dest li');
        for (var i=0; i<dest.length; i++) {
            YAHOO.util.Event.on(dest[i], 'dblclick', this.edit);
        }
    },

    edit: function(ev) {
        sorl.curator.Caption.clicked = this; // save the clicked element
        YAHOO.util.Selector.query('#caption textarea')[0].value = this.firstChild.alt; // prepopulate
        sorl.curator.Caption.dialog.show();
    },
    
    updateForm: function () {
        var imagelinks = [];
        var dest = YAHOO.util.Selector.query('#dest li');
        for (var i=0; i<dest.length; i++) {
            var img = dest[i].firstChild;
            var id = dest[i].id.replace(/\w+_/, '');
            imagelinks.push([ id, img.alt ]);
        } 
        var imagelink = YAHOO.util.Selector.query('#id__imagelinks')[0];
        imagelink.value = YAHOO.lang.JSON.stringify(imagelinks);
    },

    updateListeners: function () {
        // remove edit listeners
        for (var i=0; i<this.list_ids.length; i++){
            var elems = YAHOO.util.Selector.query('#'+ this.list_ids[i] +' li');
            for (var j=0; j<elems.length; j++) {
                YAHOO.util.Event.removeListener(elems[j], 'dblclick', this.edit);
            }
        }
        var dest = YAHOO.util.Selector.query('#dest li');
        for (var i=0; i<dest.length; i++) {
            YAHOO.util.Event.on(dest[i], 'dblclick', this.edit); // add edit listener
        } 
    },

    save: function() {
        this.updateForm();
        this.updateListeners();
    }

};

sorl.curator.Caption = {
    init: function() {
        this.dialog = new YAHOO.widget.Dialog("caption", {
            width : "30em",
            fixedcenter : true,
            visible : false, 
            constraintoviewport : true,
            buttons : [ { text:gettext("Save"), handler:this.save, isDefault:true },
                        { text:gettext("Cancel"), handler:this.cancel } ]});
        this.dialog.render();
    },

    save: function() {
        this.cancel();
        var data = this.getData();
        var caption = data.caption;
        var img = sorl.curator.Caption.clicked.firstChild; // get the clicked elements image
        img.title = caption;
        img.alt = caption; // this is where we get the caption
        YAHOO.util.Selector.query('#caption textarea')[0].value = ''
        sorl.curator.DDApp.updateForm();
    },

    cancel: function() {
        this.cancel();
        YAHOO.util.Selector.query('#caption textarea')[0].value = ''
    }
};

YAHOO.util.Event.onDOMReady(sorl.curator.DDApp.init, sorl.curator.DDApp, true);
YAHOO.util.Event.onDOMReady(sorl.curator.Caption.init, sorl.curator.Caption, true);
